package com.shanzmoo.db.module.psys.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.shanzmoo.db.module.msys.entity.MSysMenuEntity;
import com.shanzmoo.db.module.psys.entity.PSysMenuEntity;
import com.shanzmoo.db.module.psys.entity.PSysRoleMenuEntity;
import com.shanzmoo.db.module.psys.entity.PSysUserRoleEntity;
import com.shanzmoo.db.module.psys.mapper.PSysMenuMapper;
import com.shanzmoo.db.module.psys.service.IPSysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shanzmoo.db.module.psys.service.IPSysRoleMenuService;
import com.shanzmoo.db.module.psys.service.IPSysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 平台系统菜单 服务实现类
 * </p>
 *
 * @author Gzhao
 * @since 2021-04-03
 */
@Service
public class PSysMenuServiceImpl extends ServiceImpl<PSysMenuMapper, PSysMenuEntity> implements IPSysMenuService {

    /**
     * 超级管理员编号
     */
    private static final int SUPER_MANAGER_CODE = 1;

    @Autowired
    private IPSysUserRoleService sysUserRoleService;
    @Autowired
    private IPSysRoleMenuService sysRoleMenuService;

    @Override
    public List<PSysMenuEntity> getUserMenuList(Integer useId) {
        List<PSysUserRoleEntity> userRole = sysUserRoleService.lambdaQuery()
                .eq(PSysUserRoleEntity::getUserId, useId)
                .list();
        List<Integer> roleIds = userRole.stream()
                .map(PSysUserRoleEntity::getRoleId)
                .collect(Collectors.toList());

        if (CollUtil.isEmpty(roleIds)) {
            return Collections.emptyList();
        }

        // 超级管理员用于所有权限
        if (roleIds.contains(SUPER_MANAGER_CODE)) {
            return this.lambdaQuery()
                    .eq(PSysMenuEntity::getDeleted, false)
                    .list();
        }

        List<Integer> menuIds = sysRoleMenuService.lambdaQuery()
                .in(PSysRoleMenuEntity::getRoleId, roleIds)
                .list()
                .stream()
                .map(PSysRoleMenuEntity::getMenuId)
                .collect(Collectors.toList());

        List<PSysMenuEntity> menuList = this.lambdaQuery()
                .in(PSysMenuEntity::getId, menuIds)
                .eq(PSysMenuEntity::getDeleted, false)
                .list();

        // 查询父级菜单
        List<PSysMenuEntity> parentMenuList = new ArrayList<>();
        findParentMenu(menuList, parentMenuList);

        List<Integer> allMenuIds = parentMenuList.stream()
                .map(PSysMenuEntity::getId)
                .distinct()
                .collect(Collectors.toList());

        return this.lambdaQuery()
                .eq(PSysMenuEntity::getDeleted, false)
                .in(PSysMenuEntity::getId, allMenuIds)
                .list();
    }

    private void findParentMenu(List<PSysMenuEntity> menuList, List<PSysMenuEntity> allMenuList) {
        if (CollUtil.isNotEmpty(menuList)) {
            List<PSysMenuEntity> searchMenuList = menuList.stream()
                    .filter(e -> e.getParentId() != null && e.getParentId() > 0)
                    .collect(Collectors.toList());

            if (CollUtil.isNotEmpty(searchMenuList)) {
                List<Integer> parentIds = searchMenuList.stream()
                        .map(PSysMenuEntity::getParentId)
                        .collect(Collectors.toList());

                List<PSysMenuEntity> parentMenuList = this.lambdaQuery()
                        .in(PSysMenuEntity::getId, parentIds)
                        .eq(PSysMenuEntity::getDeleted, false)
                        .list();
                findParentMenu(parentMenuList, allMenuList);
            }
            allMenuList.addAll(menuList);
        }
    }
}
